package cn.worken.baseframe.common.util;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

/**
 * TODO
 * @author genxin.liu
 * @since 2021/12/28 16:52
 */
@ConditionalOnClass(RedissonClient.class)
@Component
public class RedisUtil {
    
    @Autowired(required = false)
    private RedissonClient redissonClient;
    
    /**
     * string 多key相关操作
     *
     * @author genxin.liu
     * @date 2021/12/28 17:33
     * @param:
     * @return org.redisson.api.RBuckets
     */
    public RBuckets string() {
        return redissonClient.getBuckets();
    }
    
    /**
     * string 单key相关操作
     *
     * @see RBucket#trySet = SETNX
     *
     * @author genxin.liu
     * @date 2021/12/28 17:34
     * @param: key
     * @return org.redisson.api.RBucket<V>
     */
    public <V> RBucket<V> string(String key) {
        return redissonClient.<V>getBucket(key);
    }
    
    /**
     * list 相关操作
     *
     * @author genxin.liu
     * @date 2021/12/28 17:34
     * @param: key
     * @return org.redisson.api.RList<V>
     */
    public <V> RList<V> list(String key) {
        return redissonClient.<V>getList(key);
    }
    
    /**
     * hash 相关操作
     *
     * @author genxin.liu
     * @date 2021/12/28 17:35
     * @param: key
     * @return org.redisson.api.RMap<K, V>
     */
    public <K, V> RMap<K, V> hash(String key) {
        return redissonClient.<K, V>getMap(key);
    }
    
    /**
     * set 相关操作
     *
     * @author genxin.liu
     * @date 2021/12/28 17:35
     * @param: key
     * @return org.redisson.api.RSet<V>
     */
    public <V> RSet<V> set(String key) {
        return redissonClient.getSet(key);
    }
    
    /**
     * sorted set 相关操作
     *
     * @author genxin.liu
     * @date 2021/12/28 17:35
     * @param: key
     * @return org.redisson.api.RSortedSet<V>
     */
    public <V> RSortedSet<V> sortedSet(String key) {
        return redissonClient.<V>getSortedSet(key);
    }
}
